﻿/*
 * Created by SharpDevelop.
 * User: zhangkaiyuan
 * Date: 2012-8-23
 * Time: 13:08
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.IO;
using System.Xml;
using System.Text;


namespace XML
{
	class Program
	{
		public static void Main(string[] args)
		{
			Console.WriteLine("Path:D:\\haha.xml");
			XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load("D:\\haha.xml");
            }
            catch
            {
                XmlTextWriter xtw = new XmlTextWriter("new.xml", Encoding.UTF8);
                xtw.WriteStartDocument();
                    xtw.WriteStartElement("gnode");
                          xtw.WriteStartElement("myxm1");
                          xtw.WriteAttributeString("state","California");
                          xtw.WriteAttributeString("state1","California1");
                          xtw.WriteEndElement();
                    xtw.WriteEndElement();
                xtw.WriteEndDocument();
                xtw.Close();
                doc.Load("new.xml");
            }            

            XmlNode xn = doc.DocumentElement;
            XmlElement xe = doc.CreateElement("myxml2"); 
            xe.InnerText = "hahaha";
            xn.AppendChild(xe);
            doc.Save("D:\\haha.xml");     
			Console.ReadKey(true);            
		}
	}
}